//
//  FKRefundReasonPopVC.m
//  WanTao
//
//  Created by Macbook Pro on 2019/4/2.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKRefundReasonPopVC.h"
#import "FKRefundReasonCell.h"

@interface FKRefundReasonPopVC ()<UITableViewDataSource, UITableViewDelegate, UIViewControllerTransitioningDelegate>

@property (nonatomic, weak) UIView *container;
@property (nonatomic, weak) UIView *bgView;
@property (nonatomic, weak) UITableView *tableView;

//@property (nonatomic, strong) NSArray *datas;

@end

@implementation FKRefundReasonPopVC

- (NSArray *)datas
{
    if (!_datas) {
        _datas = [NSArray array];
    }
    return _datas;
}

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    
    CGFloat containerH = fk_adjustW(420);
    CGFloat containerY = fkScreenH - containerH;
    self.view.frame = CGRectMake(0, containerY, fkScreenW, containerH);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.autoresizingMask = UIViewAutoresizingNone;
    
    self.view.backgroundColor = [UIColor clearColor];
    CGFloat containerH = fk_adjustW(420);
//    CGFloat containerY = fkScreenH - containerH;
//    self.view.frame = CGRectMake(0, containerY, fkScreenW, containerH);
//    self.view.backgroundColor = [UIColor greenColor];
    
//    CGFloat containerY = fkScreenH - containerH;
    UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, fkScreenW, containerH)];
    [self.view addSubview:container];
    self.container = container;
    container.backgroundColor = [UIColor whiteColor];
    
    UILabel *titleLabel = [UILabel fk_labelWithFont:fk_adjustFont(15) textColor:fkColor333333 text:@"退款原因"];
    [container addSubview:titleLabel];
    
    UITableView *tableView = [[UITableView alloc] init];
    [container addSubview:tableView];
    self.tableView = tableView;
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.tableFooterView = [UIView new];
    tableView.separatorInset = UIEdgeInsetsMake(0, 12, 0, 12);
    
    UIButton *confirmBtn = [UIButton fk_btnWithTarget:self action:@selector(confirmBtnClicked) font:fk_adjustFont(18) normalTitle:@"确定" normalTitleColor:[UIColor whiteColor] selectedTitle:nil selectedTitleColor:nil normalImg:nil selectedImg:nil bgImg:[UIImage imageNamed:@"bg_main"] selectedBgImg:nil];
    [container addSubview:confirmBtn];
    
    [confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.height.mas_equalTo(fk_adjustW(48));
    }];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);
        make.height.offset(fk_adjustW(45));
        make.centerX.offset(0);
    }];
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(titleLabel.mas_bottom);
        make.bottom.equalTo(confirmBtn.mas_top);
    }];
}

- (void)close
{
    [self dismissViewControllerAnimated:false completion:nil];
}

- (void)confirmBtnClicked
{
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    if (indexPath) {
        NSDictionary *data = self.datas[indexPath.row];
        if (self.selectedReason) {
            self.selectedReason(data);
        }
        [self close];
    } else {
        [SVProgressHUD showInfoWithStatus:@"请选择退款原因"];
    }
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FKRefundReasonCell *cell = [FKRefundReasonCell cellWith:tableView];
    cell.data = self.datas[indexPath.row];
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return fk_adjustW(45);
}


@end
